const Generator = require("yeoman-generator");
const fs = require("fs");

const dirArr = [];

function mapDir(dir) {
  var res = [],
    files = fs.readdirSync(dir);
  files.forEach(function (file) {
    var pathname = dir + "/" + file,
      stat = fs.lstatSync(pathname);

    if (!stat.isDirectory()) {
      res.push(pathname.substring(pathname.indexOf('/') + 1));
    } else {
      res = res.concat(mapDir(pathname));
    }
  });
  return res;
}



module.exports = class extends Generator {


  prompting() {
    const Prompt = [
      {
        type: 'input',
        name: 'name',
        default: this.appname,
        message: `请输入项目名称<${this.appname}> : `,
        validate(input) {
          if (fs.existsSync(input)) {
            return '项目名已重复!'
          }
          return true;
        }
      },
      {
        type: 'input',
        name: 'description',
        default: 'vue-base',
        message: '请输入项目描述 : ',
      },
      {
        type: 'input',
        name: 'version',
        default: '1.0.0',
        message: '请输入项目版本号<1.0.0> : ',
      },
      {
        type: 'input',
        name: 'Author',
        default: '',
        message: '作者 : ',
      }
    ]
    return this.prompt(Prompt).then((answer) => {
      this.answer = answer;
    });
  }

  writing() {
    
    const templates = mapDir(this.templatePath())

    templates.forEach(item => {
      this.fs.copyTpl(
        this.templatePath(item),
        this.destinationPath(item),
        this.answer
      )
    })
  }
};
